package com.wash.shoes.domain.mtbusy;

import com.alibaba.fastjson.JSONObject;
import com.meituan.sdk.auth.MeituanTokenData;
import com.meituan.sdk.auth.MeituanTokenResponse;
import com.meituan.sdk.internal.exceptions.MtSdkException;
import com.ruoyi.common.core.redis.RedisCache;
import com.wash.shoes.entity.MtAuthPo;
import com.wash.shoes.entity.ShopPo;
import com.wash.shoes.service.MtAuthService;
import com.wash.shoes.service.ShopService;
import com.wash.shoes.util.WashDateUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.TimeUnit;

/**
 * @author lijianguo
 * @Date 15/10/2024
 * 更新mt的token/ 一个小时执行一次
 **/
@Log4j2
@Component
public class MtUpdateToken {

    @Autowired
    RedisCache redisCache;

    @Autowired
    ShopService shopService;

    @Autowired
    MtAuthService mtAuthService;

    @PostConstruct
    void init() {
        //updateToke("202410155590600428");
    }

    /**
     * @author lijianguo
     * @Date 15/10/2024
     * 202410125916900087
     **/
    public void updateToken(String mtId) {

        String mtIdStr = redisCache.getCacheObject(mtId);
        if (mtIdStr != null) {
            log.info("授权过期,请求不对");
            return;
        }

        MtAuthPo mtAuthPo = mtAuthService.getById(mtId);
        log.info("update token info mtAuthPo is {}",JSONObject.toJSONString(mtAuthPo));
        // 获取开发者的id
        ShopPo shopPo = shopService.getByOpBizCode(mtAuthPo.getOpBizCode());
        log.info("update token info shopPo is {}",JSONObject.toJSONString(shopPo));
        try {
            MeituanTokenResponse response = MtAllClient.getClient(shopPo.getDeveloperId()).refreshToken(Integer.parseInt(mtAuthPo.getBusinessId()), mtAuthPo.getRefreshToken());
            log.info("美团 MeituanTokenResponse response {}", JSONObject.toJSONString(response));
            if (response.getCode() != 0) {
                log.error("授权失败");
                return;
            }

            MeituanTokenData tokenData = response.getData();
            // 写入数据
            MtAuthPo mtAuthPoAdd = new MtAuthPo();
            mtAuthPoAdd.setDeveloperId(mtAuthPo.getDeveloperId());
            mtAuthPoAdd.setBusinessId(mtAuthPo.getBusinessId());
            mtAuthPoAdd.setMtResponse(JSONObject.toJSONString(response));
            mtAuthPoAdd.setAccessToken(tokenData.getAccessToken());
            mtAuthPoAdd.setExpireIn(String.valueOf(tokenData.getExpireIn()));
            mtAuthPoAdd.setRefreshToken(tokenData.getRefreshToken());
            mtAuthPoAdd.setScope(tokenData.getScope());
            mtAuthPoAdd.setOpBizCode(tokenData.getOpBizCode());
            mtAuthPoAdd.setOpBizName(tokenData.getOpBizName());

            // 更新数据 授权数据
            mtAuthService.save(mtAuthPoAdd);
            // 更新授权
            shopPo = shopService.getShopByName(tokenData.getOpBizName());
            if (shopPo != null) {
                shopPo.setOpBizCode(tokenData.getOpBizCode());
                shopPo.setAuthExpireDate(WashDateUtil.addSecond(tokenData.getExpireIn()));
                shopService.updateById(shopPo);
                log.info("更新门店的授权成功 {}", JSONObject.toJSONString(shopPo));
            } else {
                log.error("门店更新失败，请检查门店是否存在");
            }
            // 120s 执行一下
            redisCache.setCacheObject(mtId, mtId, 120, TimeUnit.SECONDS);

        } catch (MtSdkException e) {
            log.error("MtSdkException {}", e);
        }

    }

}
